package edu.ycxy.poetry.service.serviceImpl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.ChaoDai;
import edu.ycxy.poetry.entity.Ju;
import edu.ycxy.poetry.entity.Writer;
import edu.ycxy.poetry.mapper.ChaoDaiMapper;
import edu.ycxy.poetry.mapper.FanYiMapper;
import edu.ycxy.poetry.mapper.JuMapper;
import edu.ycxy.poetry.mapper.WriterMapper;
import edu.ycxy.poetry.service.IJuService;
import edu.ycxy.poetry.vo.AddSentenceVo;
import edu.ycxy.poetry.vo.UpdateSentenceVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@Service
@Slf4j
public class JuServiceImpl extends ServiceImpl<JuMapper, Ju> implements IJuService {

    @Autowired
    private JuMapper juMapper;
    @Autowired
    private WriterMapper writerMapper;
    @Autowired
    private ChaoDaiMapper chaoDaiMapper;
    @Autowired
    private FanYiMapper fanYiMapper;

    // 对名句表中的数据进行查询和分页处理
    @Override
    public PageInfo<Ju> selectListJu(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Ju> jus = juMapper.selectListJu();
        PageInfo pageInfo = new PageInfo(jus);
        //返回查询结果
        return pageInfo;
    }

    //根据名句表中的gid查询该名句的作者
    @Override
    public String findNameByGid(Integer gid) {
        return juMapper.findNameByGid(gid);
    }

    //根据名句的gid查询名句内容
    @Override
    public String findTitleByGid(Integer gid,String title) {
        return juMapper.findTitleByGid(gid,title);
    }

    //获取名句的前30条名句
    @Override
    public List<Ju> selectJus() {
        return juMapper.selectJus();
    }

    /**
     * @后台
     * @param addSentenceVo
     * @return
     */
    //添加名句的方法
    @Override
    public String insert(AddSentenceVo addSentenceVo) {
        // 查询输入的作者名是否存在
        String author = addSentenceVo.getAuthor();
        Writer writer = writerMapper.findByName(author);
        log.debug("获取的作者信息： " + writer);
        log.debug("作者名字为: "  + author);
        // 当作者表存在该作者的时候,进行下一步操作
        if (writer == null && !author.isEmpty()){
           writerMapper.insertAuthor(author);
        }
        // 查询朝代是否存在,不存在的是否添加朝代
        if (chaoDaiMapper.findDynasty(addSentenceVo.getDynasty()) == null && !addSentenceVo.getDynasty().isEmpty()){
            chaoDaiMapper.addDynasty(addSentenceVo.getDynasty());
        }
        // 插入名句表
        juMapper.insertOne(addSentenceVo.getTitle(),addSentenceVo.getBiaoti(),addSentenceVo.getAuthor());
        // 当翻译内容不为空的时候，将数据插入翻译表
        if (!addSentenceVo.getReward().isEmpty()){
            fanYiMapper.insertFanYi("译文",addSentenceVo.getReward(),addSentenceVo.getBiaoti(),addSentenceVo.getAuthor());
        }
        return "successfully";
    }

    //模糊查询名句
    @Override
    public PageInfo<Ju> findListByTileToLike(String title,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Ju> listByTileToLike = juMapper.findListByTileToLike(title);
        PageInfo pageInfo = new PageInfo(listByTileToLike);

        return pageInfo;
    }

    // 根据id删除名句信息
    @Override
    public Integer deleteJuById(Integer id) {
        Integer integer = juMapper.deleteJuById(id);
        if (integer == 1){
            return 1;
        }
        return 2;
    }

    //根据id查询消息
    @Override
    public UpdateSentenceVo findOneById(Integer id) {
        Ju ju = juMapper.findOneById(id);
        UpdateSentenceVo updateSentenceVo = new UpdateSentenceVo();
        updateSentenceVo.setId(ju.getId());
        updateSentenceVo.setSentence(ju.getTitle());
        updateSentenceVo.setAuthor(juMapper.findNameByGid(ju.getGid()));
        updateSentenceVo.setPoemName(ju.getBiaoti());
        return updateSentenceVo;
    }

    // 根据id修改名句信息
    @Override
    public Integer updateJuById(UpdateSentenceVo updateSentenceVo) {
        Integer integer = juMapper.updateJuById(updateSentenceVo);
        return integer;
    }
}
